﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Collections;

namespace Demo.Calendar.Code
{
    public class Utilities
    {

        public static void addField(SPWeb web, string fieldName, SPFieldType ft, Boolean required, string columnGroup)
        {

            //Check to make sure the field dosen't alreay exsist
            if (!(web.Fields.ContainsField(fieldName)))
            {
                string newFieldName = web.Fields.Add(fieldName, ft, required);
                SPField theField = web.Fields[fieldName];
                theField.ShowInDisplayForm = true;
                theField.ShowInEditForm = true;
                theField.ShowInNewForm = true;
                theField.Group = columnGroup;
                theField.Update();
            }

        }
        public static void addDateField(SPWeb spWeb, string fieldName, SPFieldType ft, Boolean required, Boolean useToday, string columnGroup)
        {
            if (!(spWeb.Fields.ContainsField(fieldName)))
            {
                addField(spWeb, fieldName, ft, required, columnGroup);

                SPFieldDateTime dateOpenedField = (SPFieldDateTime)spWeb.Fields.GetField(fieldName);
                //// preparing to create the column with the attributes from the line above this one      
                dateOpenedField.ShowInDisplayForm = true;
                dateOpenedField.Group = columnGroup; // add the column to the group specified above      
                dateOpenedField.DisplayFormat = SPDateTimeFieldFormatType.DateOnly; // change a default parameter for this column type     
                if (useToday)
                {
                    dateOpenedField.DefaultValue = "[today]"; // change a default parameter for this column type     
                }
                dateOpenedField.Update();
            }
        }

        public static void CreateChoiceColumn(SPWeb spWeb, String name, ArrayList choices, Boolean required, String group)
        {
            if (!spWeb.Fields.ContainsField(name))
            {
                spWeb.Fields.Add(name, SPFieldType.Choice, required);
                SPFieldChoice field = (SPFieldChoice)spWeb.Fields[name];

                field.DisplaySize = choices.Count.ToString();
                foreach (String choice in choices)
                {
                    field.Choices.Add(choice);

                }
                field.PushChangesToLists = true;
                field.Group = group;
                field.Update();
                spWeb.Fields[name].Update();
            }
        }
        public static void CreateMultiChoiceColumn(SPWeb spWeb, String name, ArrayList choices, Boolean required, String group)
        {
            if (!spWeb.Fields.ContainsField(name))
            {
                spWeb.Fields.Add(name, SPFieldType.MultiChoice, required);
                SPFieldMultiChoice field = (SPFieldMultiChoice)spWeb.Fields[name];

                field.DisplaySize = choices.Count.ToString();
                foreach (String choice in choices)
                {
                    field.Choices.Add(choice);

                }
                int mycount = field.Choices.Count; //debug
                field.PushChangesToLists = true;
                field.Group = group;
                field.Update();
                spWeb.Fields[name].Update();
            }
        }

        //public static void addMMField(SPWeb spWeb, string termGroupName, string termSetName, ArrayList termValues)
        //{
        //    //I am not sure about the guids etc for these fields they ,ust be handled the same
        //    //way as lookups
        //    //string termGroupName = "ExtranetTermSets";

        //    //string BUG_TYPE = "BugType";

        //    //Dictionary of fields and their guids            
        //    Dictionary<string, string> newTermSet = new Dictionary<string, string>();
        //    foreach (string t in termValues)
        //    {
        //        newTermSet.Add(termSetName, t);
        //    }

        //    SPSite site = SPContext.Current.Site;

        //    //hook up all managed metadata columns to terms
        //    TaxonomySession session = new TaxonomySession(site);

        //    //TermStores[0] is the default, you can also pass a string of the name of the term store you want to use
        //    TermStore termstore = session.TermStores[0];
        //    Group group = termstore.Groups[termGroupName];

        //    foreach (KeyValuePair<string, string> fieldSet in newTermSet)
        //    {
        //        TermSet pbxTermSet = group.TermSets[fieldSet.Key];
        //        TaxonomyField mmField = (TaxonomyField)site.OpenWeb().Fields[new Guid(fieldSet.Value)];
        //        mmField.SspId = termstore.Id;
        //        mmField.TermSetId = pbxTermSet.Id;
        //        mmField.IsPathRendered = false;
        //        mmField.Update();
        //    }

        //}

        public static void addLookupField(SPWeb spWeb, string lookupListName, string lookupFieldName, string newFieldName, string columnGroup)
        {
            if (!(spWeb.Fields.ContainsField(newFieldName)))
            {
                SPList list = spWeb.Lists[lookupListName]; //List to which LookUp Column will be associated.
                //Name of the lookup field.
                spWeb.Fields.AddLookup(newFieldName, list.ID, list.ParentWeb.ID, false);
                spWeb.Update(); //This step is very important.
                SPFieldLookup field = spWeb.Fields[newFieldName] as SPFieldLookup;
                //field.ShowInDisplayForm = true;
                //field.ShowInEditForm = true;
                //field.ShowInNewForm = true;
                // Guid defaultView = list.DefaultView.ID;

                // field.LookupList = defaultView.ToString();
                // field.LookupField = lookupFieldName; // list.Fields[lookupFieldName].Title;//maybe need to change
                field.LookupField = list.Fields[SPBuiltInFieldId.Title].InternalName;

                //Field to which the LookUp column will be associated.
                field.Group = columnGroup;
                //Lookup Column can be assigned values from this field only.
                field.Update(true);
            }


        }


        public static void deleteField(SPWeb web, string fieldName)
        {

            SPField newField = web.Fields
            .Cast<SPField>()
            .FirstOrDefault(f => f.Title == fieldName);
            if (newField != null)
            {
                newField.Delete();
            }
            if ((web.Fields.ContainsField(fieldName)))
            {
                web.Fields.Delete(fieldName);
            }


        }

        public static void addContentTypeLink(SPWeb spWeb, string fieldName, SPContentType ct)
        {


            SPFieldLink theFieldLink = new SPFieldLink(spWeb.Fields[fieldName]);
            ct.FieldLinks.Add(theFieldLink);
            ct.Update();
        }
        public static void deleteContentTypeLink(SPWeb spWeb, string fieldName, SPContentType ct)
        {
            SPFieldLink newFieldFeldLink = new SPFieldLink(spWeb.Fields[fieldName]);
            ct.FieldLinks.Delete(newFieldFeldLink.Name);


        }

    }
}

